/*
 * ADGTFrame.java
 *
 * Created on May 11, 2011, 1:19:10 PM
 */
package com.iceberg.adgt.ui.gen;

import com.iceberg.adgt.common.Observer;
import com.iceberg.adgt.common.ObserverInterest;
import com.iceberg.adgt.domain.DomainFacade;
import com.iceberg.adgt.domain.DomainFacadeImpl;
import com.iceberg.adgt.ui.ADGTController;
import com.iceberg.adgt.ui.ADGTControllerImpl;
import com.iceberg.adgt.ui.XMLFileFilter;

import javax.swing.*;

/**
 * @author David
 */
public class ADGTFrame extends javax.swing.JFrame implements Observer {

	/**
	 * Creates new form ADGTFrame
	 */
	public ADGTFrame() {
		init();
	}

	/**
	 * This method is called from within the constructor to
	 * initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is
	 * always regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
	// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
	private void initComponents() {

		jFileChooser1 = new javax.swing.JFileChooser();
		jSeparator2 = new javax.swing.JSeparator();
		architecturePanel1 = new ArchitecturePanel();
		statusBar1 = new com.iceberg.adgt.ui.StatusBar();
		jMenuBar1 = new javax.swing.JMenuBar();
		jMenu1 = new javax.swing.JMenu();
		newArchitectureMenuItem = new javax.swing.JMenuItem();
		jSeparator3 = new javax.swing.JPopupMenu.Separator();
		openArchitectureItem = new javax.swing.JMenuItem();
		openStructureItem = new javax.swing.JMenuItem();
		jSeparator1 = new javax.swing.JPopupMenu.Separator();
		saveItem = new javax.swing.JMenuItem();
		saveAsItem = new javax.swing.JMenuItem();
		jMenu2 = new javax.swing.JMenu();
		editNameAuthorMenuItem = new javax.swing.JMenuItem();

		setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

		jMenu1.setText("Architecture");

		newArchitectureMenuItem.setText("New Architecture");
		newArchitectureMenuItem.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				newArchitectureMenuItemActionPerformed(evt);
			}
		});
		jMenu1.add(newArchitectureMenuItem);
		jMenu1.add(jSeparator3);

		openArchitectureItem.setText("Open Architecture");
		openArchitectureItem.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				openArchitectureItemActionPerformed(evt);
			}
		});
		jMenu1.add(openArchitectureItem);

		openStructureItem.setText("Open Structure");
		openStructureItem.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				openStructureItemActionPerformed(evt);
			}
		});
		jMenu1.add(openStructureItem);
		jMenu1.add(jSeparator1);

		saveItem.setText("Save");
		saveItem.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				saveItemActionPerformed(evt);
			}
		});
		jMenu1.add(saveItem);

		saveAsItem.setText("Save as");
		saveAsItem.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				saveAsItemActionPerformed(evt);
			}
		});
		jMenu1.add(saveAsItem);

		jMenuBar1.add(jMenu1);

		jMenu2.setText("Edit");

		editNameAuthorMenuItem.setText("Architecture/Structure name and author");
		editNameAuthorMenuItem.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				editNameAuthorActionPerformed(evt);
			}
		});
		jMenu2.add(editNameAuthorMenuItem);

		jMenuBar1.add(jMenu2);

		setJMenuBar(jMenuBar1);

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(
				layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(layout.createSequentialGroup()
								.addComponent(jSeparator2, javax.swing.GroupLayout.DEFAULT_SIZE, 1, Short.MAX_VALUE)
								.addGap(850, 850, 850))
						.addGroup(layout.createSequentialGroup()
								.addComponent(architecturePanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
								.addContainerGap())
						.addGroup(layout.createSequentialGroup()
								.addContainerGap()
								.addComponent(statusBar1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
								.addContainerGap(541, Short.MAX_VALUE))
		);
		layout.setVerticalGroup(
				layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(layout.createSequentialGroup()
								.addComponent(architecturePanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
								.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
								.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
										.addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
										.addGroup(layout.createSequentialGroup()
												.addComponent(statusBar1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
												.addContainerGap())))
		);

		pack();
	}// </editor-fold>//GEN-END:initComponents

	private void openArchitectureItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_openArchitectureItemActionPerformed
		int returnVal = jFileChooser1.showOpenDialog(jMenu1);

		if (returnVal == JFileChooser.APPROVE_OPTION) {
			adgtController.openArchitectureFile(jFileChooser1.getSelectedFile());
		}
	}//GEN-LAST:event_openArchitectureItemActionPerformed

	private void openStructureItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_openStructureItemActionPerformed

		int returnVal = jFileChooser1.showOpenDialog(jMenu1);

		if (returnVal == JFileChooser.APPROVE_OPTION) {
			adgtController.openStructureFile(jFileChooser1.getSelectedFile());
		}
	}//GEN-LAST:event_openStructureItemActionPerformed

	private void saveAsItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveAsItemActionPerformed

		int returnVal = jFileChooser1.showSaveDialog(jMenu1);

		if (returnVal == JFileChooser.APPROVE_OPTION) {
			adgtController.saveAs(jFileChooser1.getSelectedFile());
			this.setCustomTitle(this.adgtController.getCurrentArchitectureFileName() + " and " + this.adgtController.getCurrentStructureFileName());
			this.saveItem.setEnabled(true);
			this.statusBar1.setText("Current architecture saved");
		}
	}//GEN-LAST:event_saveAsItemActionPerformed

	private void editNameAuthorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_editNameAuthorActionPerformed

		EditNameAuthorFrame editNameAuthor = new EditNameAuthorFrame();
		editNameAuthor.setParentFrame(this);
		this.setEnabled(false);
	}//GEN-LAST:event_editNameAuthorActionPerformed

	private void newArchitectureMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_newArchitectureMenuItemActionPerformed

		facade.reset();
		this.dispose();
		new ADGTFrame().setVisible(true);
	}//GEN-LAST:event_newArchitectureMenuItemActionPerformed

	private void saveItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveItemActionPerformed
		adgtController.save();
		this.statusBar1.setText("Current architecture saved");
	}//GEN-LAST:event_saveItemActionPerformed

	// Variables declaration - do not modify//GEN-BEGIN:variables
	private ArchitecturePanel architecturePanel1;
	private javax.swing.JMenuItem editNameAuthorMenuItem;
	private javax.swing.JFileChooser jFileChooser1;
	private javax.swing.JMenu jMenu1;
	private javax.swing.JMenu jMenu2;
	private javax.swing.JMenuBar jMenuBar1;
	private javax.swing.JPopupMenu.Separator jSeparator1;
	private javax.swing.JSeparator jSeparator2;
	private javax.swing.JPopupMenu.Separator jSeparator3;
	private javax.swing.JMenuItem newArchitectureMenuItem;
	private javax.swing.JMenuItem openArchitectureItem;
	private javax.swing.JMenuItem openStructureItem;
	private javax.swing.JMenuItem saveAsItem;
	private javax.swing.JMenuItem saveItem;
	private com.iceberg.adgt.ui.StatusBar statusBar1;
	// End of variables declaration//GEN-END:variables

	private ADGTController adgtController;
	private DomainFacade facade;

	@Override
	public void notifyChange(ObserverInterest interest) {
		if (interest.equals(ObserverInterest.ARCHITECTURE_FILE_OPENED)) {
			this.saveAsItem.setEnabled(true);
			this.openStructureItem.setEnabled(true);
			this.statusBar1.setMessage("Architecture file opened");
			this.setCustomTitle(this.adgtController.getCurrentArchitectureFileName());
		} else if (interest.equals(ObserverInterest.STRUCTURE_FILE_OPENED)) {
			this.saveItem.setEnabled(true);
			this.statusBar1.setMessage("Structure file opened");
			this.setCustomTitle(this.adgtController.getCurrentArchitectureFileName() + " and " + this.adgtController.getCurrentStructureFileName());
		}
	}

	private void init() {
		initComponents();

		setCustomTitle("Untitled.xml");

		facade = DomainFacadeImpl.getInstance();
		adgtController = new ADGTControllerImpl();
		adgtController.addObserver(this, ObserverInterest.ARCHITECTURE_FILE_OPENED);
		adgtController.addObserver(this, ObserverInterest.STRUCTURE_FILE_OPENED);
		jFileChooser1.setFileFilter(new XMLFileFilter());
		openStructureItem.setEnabled(false);
		saveItem.setEnabled(false);
		statusBar1.setText("New architecture");
	}

	private void setCustomTitle(String title) {
		super.setTitle("Architecture Definition Generation Tool" + " - " + title);
	}
}
